import { BundleManager } from "./BundleManager";
import DateUtil from "./DateUtil";
import { Logger } from "./Logger";
import { RemoteAsset, RemoteAssetManager } from "./RemoteAssetManager";
import UIManager from "./UIManager";
import { UI_Config } from "./UI_Config";

export default class ResourcesManager {

    public static instance: ResourcesManager = new ResourcesManager();

    /**加载预制体 */
    public loadPrefab(uiConfig: UI_Config, callback: Function = null) {
        this.loadByBundle(uiConfig.path, uiConfig.bundleName, null, (resource: cc.Prefab) => {
            callback && callback(resource);
        })
    }

    /**
     * 加载本地图片
     * @param sprite
     * @param path
     */
    public loadSprite(sprite: cc.Sprite, uiConfig: UI_Config) {
        this.loadByBundle(uiConfig.path, uiConfig.bundleName, null, (resource: cc.Texture2D) => {
            sprite.spriteFrame = new cc.SpriteFrame(resource);
        })
    }

    public async loadByBundle(path: any, bundleName: string, progressCallback: Function, finishCallback: Function) {
        if (bundleName == BundleManager.instance.bundleName_main) {
            cc.resources.load(path, (completedCount: number, totalCount: number, item: any) => {
                progressCallback && progressCallback(completedCount, totalCount, item);
            }, (err, resource: any[]) => {
                if (err) {
                    Logger.warn("load error===", path, err)
                    return;
                }
                finishCallback && finishCallback(resource);
            });
        } else {
            let bundle: cc.AssetManager.Bundle = BundleManager.instance.bundleMap.get(bundleName);
            if (bundle) {
                bundle.load(path, (completedCount: number, totalCount: number, item: any) => {
                    progressCallback && progressCallback(completedCount, totalCount, item);
                }, (err, resource: any[]) => {
                    if (err) {
                        Logger.warn("load error===", path, bundleName, err)
                        return;
                    }
                    finishCallback && finishCallback(resource);
                });
            } else {
                Logger.error("不存在bundle==", bundleName, BundleManager.instance.bundleMap)
                // await BundleManager.instance.loadBundle(bundleName)
                this.loadByBundle(path, bundleName, progressCallback, finishCallback)
            }
        }
    }

    public load(path: any, progressCallback: Function, finishCallback: Function) {
        cc.resources.load(path, (completedCount: number, totalCount: number, item: any) => {
            progressCallback && progressCallback(completedCount, totalCount, item);
        }, (err, resource: any[]) => {
            if (err) {
                Logger.warn("load error===", path, err)
                return;
            }
            finishCallback && finishCallback(resource);
        });
    }

    public loadHttpRes(url: string = "http://1.13.82.214:9600/upload/104223-2.jpg", callback: Function = null) {
        // UIManager.instance.showLoadingIcon()
        cc.loader.load(url, (err, resource) => {
            // UIManager.instance.hideLoadingIcon()
            Logger.log("resource====", resource)
            callback && callback(resource)
        })
    }

    public loadHttpSprite(url: string, sprite: cc.Sprite, callback: Function = null) {
        let defaultWidth: number = sprite.node.width;
        let defaultHeight: number = sprite.node.height;
        // url += "?t=" + DateUtil.now()
        let remoteAsset: RemoteAsset = RemoteAssetManager.instance.cacheAsset.get(url)
        if(remoteAsset){
            let newSpriteFrame = new cc.SpriteFrame(remoteAsset.texture);
            sprite.spriteFrame = newSpriteFrame;
            callback && callback(true, remoteAsset.texture)
        }else{
            cc.assetManager.loadRemote<cc.Texture2D>(url, { ext: ".jpg" }, (err, res: cc.Texture2D) => {
                if (err) {
                    callback && callback(false)
                } else {
                    let newSpriteFrame = new cc.SpriteFrame(res);
                    sprite.spriteFrame = newSpriteFrame;
                    RemoteAssetManager.instance.insertAsset(new RemoteAsset(url, res, DateUtil.now()))
                    callback && callback(true, res)
                }
            })
        }
    }

    public releaseAsset(asset: cc.Asset) {
        cc.assetManager.releaseAsset(asset)
    }

    public releaseList(paths: string[]) {
        for (let i = 0; i < paths.length; i++) {
            this.release(paths[i]);
        }
    }

    public release(path: string) {
        cc.resources.release(path);
    }

}
